package com.jhgsys.internal.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jhgsys.internal.base.entity.BaseProjectArea;
import com.jhgsys.internal.base.mapper.BaseProjectAreaMapper;
import com.jhgsys.internal.base.service.IBaseProjectAreaService;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 项目区域 Service实现
 *
 * @author admin
 * @date 2024-07-29 15:56:11
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BaseProjectAreaServiceImpl extends ServiceImpl<BaseProjectAreaMapper, BaseProjectArea> implements IBaseProjectAreaService {

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-07-29 15:56:11
     * @param request QueryRequest
     * @param baseProjectArea baseProjectArea
     * @return IPage<BaseProjectArea>
    */
    @Override
    public IPage<BaseProjectArea> findBaseProjectAreas(QueryRequest request, BaseProjectArea baseProjectArea) {
        LambdaQueryWrapper<BaseProjectArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseProjectArea::getDeletedFlg, FebsConstant.VALID);
        // TODO 设置查询条件

        Page<BaseProjectArea> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-07-29 15:56:11
     * @param baseProjectArea baseProjectArea
     * @return List<BaseProjectArea>
     */
    @Override
    public List<BaseProjectArea> findBaseProjectAreas(BaseProjectArea baseProjectArea) {
	    LambdaQueryWrapper<BaseProjectArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseProjectArea::getDeletedFlg, FebsConstant.VALID);
        if(StringUtils.isNotBlank(baseProjectArea.getProjectId())){
            queryWrapper.eq(BaseProjectArea::getProjectId, baseProjectArea.getProjectId());
        }
        if(StringUtils.isNotBlank(baseProjectArea.getTenantId())){
            queryWrapper.eq(BaseProjectArea::getTenantId, baseProjectArea.getTenantId());
        }
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据项目project_id查询
     * @author admin
     * @date 2024-08-05 15:04:11
     * @param projectIds
     * @return List<BaseProjectArea>
     */
    @Override
    public List<BaseProjectArea> findByProjectIds(List<String> projectIds) {
	    LambdaQueryWrapper<BaseProjectArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseProjectArea::getDeletedFlg, FebsConstant.VALID);
        queryWrapper.in(BaseProjectArea::getProjectId,projectIds);
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author admin
    * @date 2024-07-29 15:56:11
    * @param id
    * @return BaseProjectArea
    */
    @Override
    public BaseProjectArea findBaseProjectAreaById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author admin
     * @date 2024-07-29 15:56:11
     * @param baseProjectArea baseProjectArea
     */
    @Override
    @Transactional
    public void createBaseProjectArea(BaseProjectArea baseProjectArea) {
        baseProjectArea.setCreatedTime(new Date());
        this.save(baseProjectArea);
    }

    /**
     * 修改
     * @author admin
     * @date 2024-07-29 15:56:11
     * @param baseProjectArea baseProjectArea
     */
    @Override
    @Transactional
    public void updateBaseProjectArea(BaseProjectArea baseProjectArea) {
        baseProjectArea.setUpdatedTime(new Date());
        this.saveOrUpdate(baseProjectArea);
    }

    /**
     * 逻辑删除
     * @author admin
     * @date 2024-07-29 15:56:11
     * @param baseProjectArea baseProjectArea
     * @param baseProjectAreaIds baseProjectAreaIds
     */
    @Override
    @Transactional
    public void deleteBaseProjectArea(BaseProjectArea baseProjectArea,String baseProjectAreaIds) {
        List<String> list = Arrays.asList(baseProjectAreaIds.split(StringPool.COMMA));
        this.baseMapper.update(baseProjectArea,new LambdaQueryWrapper<BaseProjectArea>().in(BaseProjectArea::getProjectAreaId,list));
	}

    /**
    * 物理删除
    * @author admin
    * @date 2024-07-29 15:56:11
    * @param baseProjectAreaIds baseProjectAreaIds
    */
    @Override
    @Transactional
    public void physicallyDeleteBaseProjectArea(String baseProjectAreaIds) {
        List<String> list = Arrays.asList(baseProjectAreaIds.split(StringPool.COMMA));
        LambdaQueryWrapper<BaseProjectArea> wapper = new LambdaQueryWrapper<>();
        wapper.in(BaseProjectArea::getProjectAreaId,list);
        this.remove(wapper);
    }

    /**
     * 根据项目id删除
     * @author admin
     * @date 2024-07-29 15:56:15
     * @param projectIds
     */
    @Override
    @Transactional
    public void deleteByProjectIds(String projectIds) {
        List<String> list = Arrays.asList(projectIds.split(StringPool.COMMA));
        LambdaQueryWrapper<BaseProjectArea> wapper = new LambdaQueryWrapper<>();
        wapper.in(BaseProjectArea::getProjectId,list);
        this.remove(wapper);
    }
}
